// pages/main/purchaseOrder/buyfinishedgoodsInfo.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        buyInfo: {}, //采购单信息
        url: getApp().globalData.apiurl,
        weighingInfo: null, //过磅信息       
        weighingList: [],
        listData: [],
        // driverList: [], // 车辆信息

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 得到当前数据条id
        let id = options.buyId;
        this.getInfoById(id)
    },


    getInfoById(id) {
        let _that = this;
        // 根据数据id获取采购单信息
        wx.request({
            url: _that.data.url + 'buyfinishedgoods/buyfinishedgoods/' + id,
            method: 'get',
            success: res => {
                // _that.data.userinfo = res.data.data;
                _that.setData({
                    buyInfo: res.data.data,
                    listData: [res.data.data]
                });
                // console.log(this.data.buyInfo)
            }
        });
        // 根据采购单id获取过磅信息  /logmanagement/weighingrecords
        wx.request({
            url: _that.data.url + 'logmanagement/weighingrecords/list',
            method: 'get',
            data: {
                type: 1,
                orderid: id
            },
            success: res => {
                _that.setData({
                    weighingInfo: res.data.rows[0],
                    weighingList: res.data.rows
                })
                console.log('过磅数据', this.data.weighingList)
            },
            complete: res => {
                // 根据采购单id 获取司机信息
                wx.request({
                    url: _that.data.url + 'driverorders/driverorders/list',
                    method: 'get',
                    data: {
                        type: 1,
                        orderid: id
                    },
                    success: res => {
                        let ordernetWeight = 0;
                        let dri = res.data.rows;
                        dri.forEach(item => {
                            if (item.gbInfo == undefined) {
                                item.gbInfo = []
                            }
                            _that.data.weighingList.forEach(wtem => {
                                console.log(wtem.licenseplate.toUpperCase())
                                if (wtem.licenseplate.toUpperCase() === item.licenseplate.toUpperCase()) {
                                    item.gbInfo.push(wtem);
                                    ordernetWeight += wtem.netweight;
                                }
                            })
                            // item.gbInfo.push(_that.data.weighingList.find(wtem => wtem.licenseplate === item.licenseplate))
                            // if (item.gbInfo != undefined) {
                            //     ordernetWeight += item.gbInfo.netweight
                            // }
                            console.log(item.gbInfo)
                        })
                        _that.setData({
                            driverList: res.data.rows,
                            ordernetWeight: ordernetWeight
                        })
                    }
                });
            }
        });

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})